<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header">
        <span>设备类型故障统计</span>
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          @change="handleDateChange"
          style="float: right; width: 300px;"
        ></el-date-picker>
      </div>
      <div id="failureStatisticsChart" style="width: 100%; height: 400px;" v-loading="loading"></div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import { getFailureStatistics } from '@/api/equipment/analysis';

export default {
  name: "EquipmentAnalysis",
  data() {
    return {
      dateRange: [],
      loading: false,
      failureStatisticsChart: null
    };
  },
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      this.failureStatisticsChart = echarts.init(document.getElementById('failureStatisticsChart'));
      // 监听窗口大小变化，调整图表大小
      window.addEventListener('resize', () => {
        this.failureStatisticsChart.resize();
      });
    },
    handleDateChange() {
      this.loadFailureStatistics();
    },
    loadFailureStatistics() {
      // 验证日期范围
      if (this.dateRange && this.dateRange.length === 2) {
        this.loading = true;
        const params = {
          startDate: this.dateRange[0],
          endDate: this.dateRange[1]
        };
        getFailureStatistics(params).then(response => {
          const data = response.data;
          if (!data || !data.source || data.source.length === 0) {
            this.$message.warning('所选日期范围内没有数据');
            return;
          }
          
          const option = {
            title: {
              text: '设备类型故障统计',
              left: 'center'
            },
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'shadow'
              },
              formatter: '{b}: {c} 次'
            },
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            xAxis: {
              type: 'category',
              data: data.source.map(item => item['设备类型']),
              axisLabel: {
                interval: 0,
                rotate: 30
              }
            },
            yAxis: {
              type: 'value',
              name: '故障次数',
              nameTextStyle: {
                padding: [0, 0, 0, 40]
              }
            },
            series: [
              {
                name: '故障次数',
                type: 'bar',
                data: data.source.map(item => item['故障次数']),
                itemStyle: {
                  color: '#409EFF'
                },
                label: {
                  show: true,
                  position: 'top'
                }
              }
            ]
          };
          this.failureStatisticsChart.setOption(option);
        }).catch(error => {
          this.$message.error('获取数据失败：' + error.message);
        }).finally(() => {
          this.loading = false;
        });
      } else {
        this.$message.warning('请选择日期范围');
      }
    }
  },
  beforeDestroy() {
    // 组件销毁前移除事件监听
    window.removeEventListener('resize', () => {
      this.failureStatisticsChart.resize();
    });
    // 销毁图表实例
    if (this.failureStatisticsChart) {
      this.failureStatisticsChart.dispose();
    }
  }
};
</script>

<style scoped>
.box-card {
  margin-bottom: 20px;
}
</style> 